<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>工资核算</title>
<meta name="renderer" content="webkit">	
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">	
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">	
<meta name="apple-mobile-web-app-status-bar-style" content="black">	
<meta name="apple-mobile-web-app-capable" content="yes">	
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/index.css">
<link rel="stylesheet" href="css/table.css">
</head>
<body>
	<div class="content-head">悦宝园会员管理系统</div>
	<div class="content-main">
		<div class="side-nav" ms-controller="navBar">
			<ul class="nav-item">
				<li><a href="main.html" class="nav-item-front nav-item-add">首页</a></li>
				<li ms-for="el in @navTitleArr">
					<a href="javascript:;" class="nav-item-front">{{el.par}}</a>
					<dl class="nav-item-child">
						<dd ms-for="($index, ex) in el.child"><a ms-attr='{href: el.href[$index]}'>{{ex}}</a></dd>
					</dl>
				</li>
			</ul>
		</div>
		<div class="content bg-gray" ms-controller="main">
			<div class="tab-content">
				<div class="tab-item">
					<div class="sub-main">
						<p class="sub-title"><i>工资核算</i></p>
						<div class="search-area">
							<div class="search-block">
								<span class="search-name">类型：</span>
								<div class="search-input">
									<select id="type" ms-duplex-checked="@typeCheck">
										<option value="cc" selected="selected">顾问</option>
										<option value="teacher">老师</option>
									</select>
								</div>
							</div>
							<div class="search-block">
								<span class="search-name">时间：</span>
								<div class="search-input">
									<select id="year">
										<option value="2019">2019</option>
										<option value="2020">2020</option>
										<option value="2021">2021</option>
									</select>
									<span class="search-name" style="padding: 0 10px;">年</span>
									<select id="month">
										<option value="1">1</option>
										<option value="2">2</option>
										<option value="3">3</option>
										<option value="4">4</option>
										<option value="5">5</option>
										<option value="6">6</option>
										<option value="7">7</option>
										<option value="8">8</option>
										<option value="9">9</option>
										<option value="10">10</option>
										<option value="11">11</option>
										<option value="12">12</option>
									</select>
									<span class="search-name" style="padding: 0 10px;">月</span>
									<select id="day">
										<option value="0">不限</option>
										<option value="1">1</option>
										<option value="2">2</option>
										<option value="3">3</option>
										<option value="4">4</option>
										<option value="5">5</option>
										<option value="6">6</option>
										<option value="7">7</option>
										<option value="8">8</option>
										<option value="9">9</option>
										<option value="10">10</option>
										<option value="11">11</option>
										<option value="12">12</option>
										<option value="13">13</option>
										<option value="14">14</option>
										<option value="15">15</option>
										<option value="16">16</option>
										<option value="17">17</option>
										<option value="18">18</option>
										<option value="19">19</option>
										<option value="20">20</option>
										<option value="21">21</option>
										<option value="22">22</option>
										<option value="23">23</option>
										<option value="24">24</option>
										<option value="25">25</option>
										<option value="26">26</option>
										<option value="27">27</option>
										<option value="28">28</option>
										<option value="29">29</option>
										<option value="30">30</option>
										<option value="31">31</option>
									</select>
									<span class="search-name" style="padding: 0 10px;">日</span>
								</div>
							</div>
							<div class="search-block">
								<a href="javascript:;" class="search-btn" ms-on-click="searchData1()">搜索</a>
							</div>
						</div>
						<div class="table-area hide"  id="teacher-box">
							<div class="table-head">
								<span class="col-15">姓名</span>
								<span class="col-15">人头数（单）</span>
								<span class="col-15">人头数（双主）</span>
								<span class="col-15">人头数（双助）</span>
								<span class="col-15">成单（主）</span>
								<span class="col-15">成单（助）</span>
								<span class="col-10">总计</span>
							</div>
							<div class="table-body">
								<div class="table-line" ms-for="(key, el) in @data2">
									<div class="col-padding col-border col-inline col-center col-15">{{key}}</div>
									<div class="col-padding col-border col-inline col-center col-15">{{el.single || 0}}（{{el.single*10 || 0}}）</div>
									<div class="col-padding col-border col-inline col-center col-15">{{el.twinMain || 0}}（{{el.twinMain*7 || 0}}）</div>
									<div class="col-padding col-border col-inline col-center col-15">{{el.twinSub || 0}}（{{el.twinSub*6 || 0}}）</div>
									<div class="col-padding col-border col-inline col-center col-15">{{el.buyMain || 0}}（{{(el.buyMain*75)/10000 || 0}}）</div>
									<div class="col-padding col-border col-inline col-center col-15">{{el.buySub || 0}}（{{(el.buySub*5) / 10000 || 0}}）</div>
									<div class="col-padding col-border col-inline col-center col-10" ms-html="@calc(el)"></div>
								</div>
							</div>
						</div>
						<div class="table-area" id="cc-box">
							<div class="table-head">
								<span class="col-25">姓名</span>
								<span class="col-25">续课</span>
								<span class="col-25">新单</span>
								<span class="col-25">总计</span>
							</div>
							<div class="table-body">
								<div class="table-line" ms-for="(key, el) in @data1">
									<div class="col-padding col-border col-inline col-center col-25">{{key}}</div>
									<div class="col-padding col-border col-inline col-center col-25" ms-html="@addClasses(el)"></div>
									<div class="col-padding col-border col-inline col-center col-25" ms-html="@newClasses(el)"></div>
									<div class="col-padding col-border col-inline col-center col-25" ms-html="@calc1(el)"></div>
								</div>
							</div>
						</div>
						<div class="table-area" style="margin-top: 20px;">
							<div class="table-body">
								<div class="table-line">
									<div class="col-padding col-inline col-center col-33">会员消课数：{{memberIds}}</div>
									<div class="col-padding col-inline col-center col-33">试听：{{rememberIds}}</div>
									<div class="col-padding col-inline col-center col-33">成单数：{{buyIds}}</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
<script src="js/jq.js"></script>
<script src="js/avalon.js"></script>
<script src="js/main.js"></script>
<script>
(function() {
	var vm = avalon.define({
		$id: "main",
		allData: [],
		data1: {},	
		data2: {},		
		memberIds: 0,
		rememberIds: 0,
		buyIds: 0,
		target: 43000,
		typeCheck: "cc",
		calc: function(el) {
			var a = el.single || 0,
				b = el.twinMain || 0,
				c = el.twinSub || 0,
				d = el.buyMain || 0,
				e = el.buySub || 0;

			return (a*10 + b*7 + c*6 + (d*75) / 10000 + (e*5) / 10000).toFixed(2);
		},	
		addClasses: function(el) {
			var ad = el.addClass,
				num = 0;
			for (var i = 0; i < ad.length; i++) {
				num += parseInt(ad[i].price);
			}
			return num;
		},
		newClasses: function(el) {
			var ad = el.newClass,
				num = 0;
			for (var i = 0; i < ad.length; i++) {
				num += parseInt(ad[i].price);
			}
			return num;
		},
		calc1: function(el) {
			var ad = el.addClass,
				ne = el.newClass,
				adPrice = 0,
				nePrice = 0,
				levelNum = 0,
				levelPer = 0,
				num = 0,
				out = false,
				total = 0,
				totalNum = 0,
				s120 = 51600,
				s140 = 60200;
			for (var i = 0; i < ad.length; i++) {
				adPrice += parseInt(ad[i].price);
			}
			for (var i = 0; i < ne.length; i++) {
				nePrice += parseInt(ne[i].price);
			}

			levelNum = adPrice + nePrice;

			if(levelNum < 12900 ) {
				levelPer = 1;
			} else if(levelNum >= 12900 && levelNum < 25800) {
				levelPer = 2;
			} else if(levelNum >= 25800 && levelNum < 43000 ) {
				levelPer = 3;
			} else if(levelNum >= 43000 && levelNum < 51600 ) {
				levelPer = 4;
			} else {
				levelPer = 4;
				out = true;
			}

			adPrice = (adPrice * 7) / 10;
			totalNum = adPrice + nePrice;

			if(out) {
				if(totalNum > s120 && totalNum < s140) {
					total = (totalNum * levelPer) /100 + ((totalNum - s120) * 6) / 100;
					return total.toFixed(2);
				}

				if(levelNum >= s140) {
					var s = (totalNum - s120 - (totalNum - s140));
					var f = totalNum - s140
					total = ((s120 * levelPer) / 100) + ((totalNum - s120 - (totalNum - s140)) * 6) / 100 +((totalNum - s140) * 8) / 100;
					return total.toFixed(2);
				}

				
			}

			total = (totalNum * levelPer) / 100;
			return total.toFixed(2);
		},
		ccFunc: function(year, month, day, tempData) {
			var objs = {},
				buyInfo,
				buyDay,
				date,
				cc;
			for (var i = 0; i < tempData.length; i++) {
				buyInfo = tempData[i].buyInfo; //续课
				buyDay = tempData[i].buyDay;  //新报

				if(!tempData[i].cc) {
					continue;
				}

				if (buyDay) { //新单
					date = buyDay.split("-");
					if(day == 0) {
						if(date[0] == year && date[1] == month) {
							cc = tempData[i].cc;
							if(!objs[cc]) {
								objs[cc] = {newClass: [], addClass: []};
							}
							
							objs[cc]["newClass"].push({name: tempData[i].name, date: tempData[i].buyDay, price: tempData[i].price || 0});
						}
					} else {
						if(date[0] == year && date[1] == month && date[2] == day) {
							cc = tempData[i].cc;
							if(!objs[cc]) {
								objs[cc] = {newClass: [], addClass: []};
							}
							
							objs[cc]["newClass"].push({name: tempData[i].name, date: tempData[i].buyDay, price: tempData[i].price || 0});
						}
					}
				}
				for (var n = 0; n < buyInfo.length; n++) {
					if(buyInfo[n].date.indexOf(".") > -1 || buyInfo[n].price == 9999 || buyInfo[n].price == 99999 || buyInfo[n].price == 999) {
						continue;
					}
					date = buyInfo[n].date.split("-");
					if(date.length == 1) {
						continue;
					}
					if(day == 0) {
						if(date[0] == year && date[1] == month) {
							cc = tempData[i].cc;
							if(!objs[cc]) {
								objs[cc] = {newClass: [], addClass: []};
							}
							
							objs[cc]["addClass"].push({name: tempData[i].name, date: buyInfo[n].date, price: buyInfo[n].price || 0});
						}
					} else {
						if(date[0] == year && date[1] == month && date[2] == day) {
							cc = tempData[i].cc;
							if(!objs[cc]) {
								objs[cc] = {newClass: [], addClass: []};
							}
							
							objs[cc]["addClass"].push({name: tempData[i].name, date: buyInfo[n].date, price: buyInfo[n].price || 0});
						}
					}
				}
			}

			var keys = Object.keys(objs);
			if(!keys.length) {
				baseFunc.alert("无数据");
			}

			this.data1 = objs;
		},
		teacherFunc: function(year, month, day) {
			var tempArr = [],
				teachers = {},
				tempData = baseFunc.getStorageData("schedule");

			vm.memberIds = 0;
			vm.rememberIds = 0;
			vm.buyIds = 0;

			for (var i = 0; i < tempData.length; i++) {
				var date = tempData[i]["date"].split("-");
				if(day == 0) {
					if(date[0] == year && date[1] == month && tempData[i].passStatus) {
						tempArr.push(tempData[i]);
					}
				} else {
					if(date[0] == year && date[1] == month && date[2] == day && tempData[i].passStatus) {
						tempArr.push(tempData[i]);
					}
				}
			}

			var data = tempArr;
				//[{single: "", twinMain: "", twinSub: "", buyMain: "", buySub: ""}]
			for (var i = 0; i < data.length; i++) { //天
				var infoData = data[i].info;
				for (var k = 0; k < infoData.length; k++) { //info
					if(infoData[k].students.length > 0) {
						if(infoData[k].teachers.length == 2) { //双老师
							var mainTeacher = infoData[k].teachers[0].name,
								subTeacher = infoData[k].teachers[1].name,
								students = infoData[k].students,
								memberIds = 0,
								rememberIds = 0,
								buyMoney = 0;
							for (var m = 0; m < students.length; m++) { //students
								if(students[m].type == "member") { //会员
									if(students[m].name == "赵泽嘉" || students[m].name == "赵晨熙" || students[m].name == "赵瑀辰" || students[m].name == "辛怡文") {
										continue;
									}
									memberIds++;
									vm.memberIds++;
								} else if (students[m].type == "reMember" && students[m].isMoney) { //成交
									buyMoney += parseInt(students[m].price);
									vm.rememberIds++;
									vm.buyIds++;
								} else { //未成交非会员
									rememberIds++;
									vm.rememberIds++;
								}
							}
							if(!teachers[mainTeacher]) {
								teachers[mainTeacher] = {};
							}
							if(!teachers[subTeacher]) {
								teachers[subTeacher] = {};
							}
							teachers[mainTeacher]["twinMain"] = parseInt(teachers[mainTeacher]["twinMain"] || 0) + memberIds + rememberIds;
							teachers[mainTeacher]["buyMain"] = parseInt(teachers[mainTeacher]["buyMain"] || 0) + buyMoney;
							teachers[subTeacher]["twinSub"] = parseInt(teachers[mainTeacher]["twinSub"] || 0) + memberIds + rememberIds;
							teachers[subTeacher]["buySub"] = parseInt(teachers[mainTeacher]["buySub"] || 0) + buyMoney;
						} else { //单老师
							var mainTeacher = infoData[k].teachers[0].name,
								students = infoData[k].students,
								memberIds = 0,
								rememberIds = 0,
								buyMoney = 0;
							for (var m = 0; m < students.length; m++) { //students
								if(students[m].type == "member") { //会员
									if(students[m].name == "赵泽嘉" || students[m].name == "赵晨熙" || students[m].name == "赵瑀辰" || students[m].name == "辛怡文" || students[m].name == "何景逸") {
										continue;
									}
									memberIds++;
									vm.memberIds++;
								} else if (students[m].type == "reMember" && students[m].isMoney) { //成交
									buyMoney += parseInt(students[m].price);
									vm.rememberIds++;
									vm.buyIds++;
								} else { //未成交非会员
									rememberIds++;
									vm.rememberIds++;
								}
							}
							if(!teachers[mainTeacher]) {
								teachers[mainTeacher] = {};
							}
							if(!teachers[subTeacher]) {
								teachers[subTeacher] = {};
							}
							teachers[mainTeacher]["single"] = parseInt(teachers[mainTeacher]["single"] || 0) + memberIds + rememberIds;
							teachers[mainTeacher]["buySub"] = parseInt(teachers[mainTeacher]["buySub"] || 0) + buyMoney;
						}
					}
				}
			}

			var keys = Object.keys(teachers);
			if(!keys.length) {
				baseFunc.alert("无数据");
			}
			
			this.data2 = teachers;
		},
		searchData1: function(){
			var year = $("#year option:selected").val(),
				month = $("#month option:selected").val(),
				day = $("#day option:selected").val(),
				tempData = baseFunc.getStorageData("member"),
				type = this.typeCheck;

			if(type == "cc") {
				$("#teacher-box").addClass("hide");
				$("#cc-box").addClass("hide");
				$("#cc-box").removeClass("hide");
				this.data1 = {};
				this.ccFunc(year, month, day, tempData);
			} else {
				$("#teacher-box").addClass("hide");
				$("#cc-box").addClass("hide");
				$("#teacher-box").removeClass("hide");
				this.data2 = {};
				this.teacherFunc(year, month, day);
			}
		}
	});

	vm.allData = baseFunc.getStorageData("member");
})();
</script>
</body>
</html>